//
//  LLSavedMusicViewController.swift
//  MyEcho
//
//  Created by longlong on 16/2/1.
//  Copyright © 2016年 LongLong. All rights reserved.
//

import UIKit

class LLSavedMusicViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    @IBOutlet weak var myTableVIew: UITableView!
    
    var tableAry = NSMutableArray()
    override func viewDidLoad() {
        super.viewDidLoad()
        tableAry = Musics.getAllDownMuics();
        NSNotificationCenter.defaultCenter().addObserver(self, selector:Selector("newMusic"), name: "newMusic", object: nil)

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func newMusic(){
        tableAry = Musics.getAllDownMuics();
        myTableVIew.reloadData();
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableAry.count;
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellID = "downMusic";
        var cell = tableView.dequeueReusableCellWithIdentifier(cellID)
        if cell==nil{
            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellID)
        }
        let mus = tableAry[indexPath.row] as! Musics
//        let url = NSURL(string: mus.pic_200!);
//        cell?.imageView?.sd_setImageWithURL(url, placeholderImage: UIImage(named: "default"))
        cell?.textLabel?.font = UIFont.systemFontOfSize(13)
        cell?.textLabel?.text = mus.musicName;
        return cell!;
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 50;
    }
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true);
        //进入本地播放
        GlobalDataObject.sharedInstance.isFromNetWork = false;
        GlobalDataObject.sharedInstance.locationAry = tableAry
        GlobalDataObject.sharedInstance.currentIndex = indexPath.row
        
        self.mm_drawerController.closeDrawerAnimated(true) { (isOK) -> Void in
            let nav =  self.mm_drawerController.centerViewController as! UINavigationController;
            let viewController = nav.viewControllers[0] as! LLMainViewController;
            if nav.viewControllers.count>1{
                //在播放页面
                let viewControlller = nav.viewControllers[1] as UIViewController
                viewControlller.viewWillAppear(true)
                return
            }else if GlobalDataObject.sharedInstance.arySave.count==1{
                viewController.navigationController?.pushViewController( GlobalDataObject.sharedInstance.arySave.firstObject as! UIViewController, animated: true);
            }else{
                viewController.performSegueWithIdentifier("GoPlay", sender: nil);
            }
        }
    }
}
